package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CusPositions;
import com.ruoyi.system.service.ICusPositionsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-01-12
 */
@Controller
@RequestMapping("/system/positions")
public class CusPositionsController extends BaseController
{
    private String prefix = "system/positions";

    @Autowired
    private ICusPositionsService cusPositionsService;

    @RequiresPermissions("system:positions:view")
    @GetMapping()
    public String positions()
    {
        return prefix + "/positions";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:positions:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(CusPositions cusPositions)
    {
    	if(cusPositions.getParams().get("selectpage") != null) {
    		List<CusPositions> list = cusPositionsService.selectCusPositionsList(cusPositions);
    		getSelect2(list, new Function<CusPositions, HashMap<String,String>>() {

				@Override
				public HashMap<String, String> apply(CusPositions t) {
					HashMap<String, String> data = new HashMap<>();
					data.put("id", t.getID() + "");
					data.put("text", t.getPositionName());
					return data;
				}
			});
    	}
        startPage();
        List<CusPositions> list = cusPositionsService.selectCusPositionsList(cusPositions);
        return getDataTable(list);
    }
    
    /**
     * 查询列表（select2）
     */
    //@RequiresPermissions("system:positions:list")
    @RequestMapping("/select2")
    @ResponseBody
    public Object select2(CusPositions cusPositions, String q)
    {
    	cusPositions.setPositionName(q);
    	
    	List<CusPositions> list = cusPositionsService.selectCusPositionsList(cusPositions);
		return getSelect2(list, new Function<CusPositions, HashMap<String,String>>() {
			@Override
			public HashMap<String, String> apply(CusPositions t) {
				HashMap<String, String> data = new HashMap<>();
				data.put("id", t.getID() + "");
				data.put("text", t.getPositionName());
				
				return data;
			}
		});
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:positions:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CusPositions cusPositions)
    {
        List<CusPositions> list = cusPositionsService.selectCusPositionsList(cusPositions);
        ExcelUtil<CusPositions> util = new ExcelUtil<CusPositions>(CusPositions.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:positions:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CusPositions cusPositions)
    {
        return toAjax(cusPositionsService.insertCusPositions(cusPositions));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        CusPositions cusPositions = cusPositionsService.selectCusPositionsByID(ID);
        mmap.put("cusPositions", cusPositions);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:positions:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CusPositions cusPositions)
    {
        return toAjax(cusPositionsService.updateCusPositions(cusPositions));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:positions:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cusPositionsService.deleteCusPositionsByIDs(ids));
    }
}
